/*
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
*/

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        if (board.size() != 9 || board[0].size() != 9) return false;
        // check row
        for (int i = 0; i < 9; i++) {
            vector<int> nums(10, 0);
            for (int j = 0; j < 9; j++) {
                if (board[i][j] == '.') continue;
                int idx = board[i][j] - '0';
                if (nums[idx]) return false;
                nums[idx] = 1;
            }
        }
        // check column
        for (int j = 0; j < 9; j++) {
            vector<int> nums(10, 0);
            for (int i = 0; i < 9; i++) {
                if (board[i][j] == '.') continue;
                int idx = board[i][j] - '0';
                if (nums[idx]) return false;
                nums[idx] = 1;
            }
        }
        // check individual blocks
        for (int ite = 0; ite < 9; ite++) {
            int mulx = ite / 3;
            int muly = ite % 3;
            vector<int> nums(10, 0);
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    int x = mulx*3+i; int y = muly*3+j;
                    if (board[x][y] == '.') continue;
                    int idx = board[x][y] - '0';
                    if (nums[idx]) return false;
                    nums[idx] = 1;
                }
            }
        }
        return true;
    }
};
